package edu.gatech.oops.servlets;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class AddUser
 * This class acts as the servlet to help handle the adding of a new user to the system.
 */
public class AddUser extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AddUser() {
		super();
		// TODO Auto-generated constructor stub
	}
	/**
	 * This method takes in the request from the CreateUser class
	 * Takes in the name, username, password and email and the type
	 * Based on the users input it creates a user object.
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		// PrintWriter out = response.getWriter();
		String name = (String) request.getParameter("txtName");
		String userName = (String) request.getParameter("txtUsername");
		String password = (String) request.getParameter("txtPassword");
		String email = (String) request.getParameter("txtEmail");
		String type = (String) request.getParameter("type");
		if (type.equalsIgnoreCase("Student")) {
			Student myStudent = new Student(name, userName, password, email);
		} else if (type.equalsIgnoreCase("Instructor")) {
			Instructor myInstructor = new Instructor(name, userName, password,
					email);
		}
		File file = new File("/home/pushkara/workspace/LabAssistan/Data.txt");
		BufferedWriter writer = new BufferedWriter(new FileWriter(file, true));
		// writer.write(name+","+email);
		writer.write(userName + "," + password + "," + name + "," + email + ","
				+ type+"\n");
		writer.close();
		response.sendRedirect("ThankYou.jsp");

	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
		// TODO Auto-generated method stub
	}

}
